# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api

import logging
_logger = logging.getLogger(__name__)


class Company(models.Model):
    _inherit = 'res.company'

    iot_project_ids = fields.One2many(
        'iot.project', 'company_id', string='Iot Projects')
    iot_gateway_ids = fields.One2many(
        'iot.gateway', 'company_id', string='Iot Gateways')
    iot_device_ids = fields.One2many(
        'iot.device', 'company_id', string='Iot Devices')
    iot_sensor_ids = fields.One2many(
        'iot.sensor', 'company_id', string='Iot Sensors')


class IotSoilType(models.Model):
    _name = "iot.soil.type"
    _description = 'Soil Type'
    _order = "id"
    name = fields.Char(index=True)
    code = fields.Char(index=True)

    field_capacity = fields.Float(
        string='Field Capacity', digits=(16, 5), default=0.0)
    total_available_water = fields.Float(
        string='Total Available Water', digits=(16, 5), default=0.0)
    pwp = fields.Float(string='PWP', digits=(16, 5), default=0.0)


class IotCropType(models.Model):
    _name = "iot.crop.type"
    _description = 'Crop Type'
    _order = "id"
    name = fields.Char(index=True)
    code = fields.Char(index=True)

    madv = fields.Float(string='madv', digits=(16, 5), default=0.0)


class IotProject(models.Model):
    _name = "iot.project"
    _description = 'IotProject'
    _order = "name"

    name = fields.Char(index=True)
    code = fields.Char(index=True)
    user_id = fields.Many2one('res.users', 'User',
                              default=lambda self: self.env.uid, index=True)

    partner_id = fields.Many2one('res.partner', 'Partner',
                                 related='user_id.partner_id', store=True, index=True)

    company_id = fields.Many2one('res.company', 'Company',
                                 default=lambda self: self.env.company, index=True)

    gateway_ids = fields.One2many(
        'iot.gateway', 'project_id', string='Gateways')
    device_ids = fields.One2many('iot.device', 'project_id', string='Devices')
    sensor_ids = fields.One2many('iot.sensor', 'project_id', string='Sensors')

    user_ids = fields.Many2many('res.users', string='Shared Users')

    # partner_ids = fields.Many2many('res.partner', string='Shared Partners' )

    soil_type_id = fields.Many2one('iot.soil.type', 'soil_type_id')
    crop_type_id = fields.Many2one('iot.crop.type', 'crop_type_id')

    field_capacity = fields.Float(
        string='Field Capacity', related="soil_type_id.field_capacity")

    total_available_water = fields.Float(
        string='Total Available Water', related="soil_type_id.total_available_water")

    pwp = fields.Float(
        string='pwp', related="soil_type_id.pwp")

    madv = fields.Float(string='madv', related="crop_type_id.madv")

    mad = fields.Float(
        string='Management Allowable Depletion', compute='_compute_mad')

    def _compute_mad(self):
        for rec in self:
            rec.mad = rec.field_capacity - (
                rec.total_available_water * rec.madv)

    start_date = fields.Date('Start Date', default=fields.Date.context_today)
    end_date = fields.Date('End Date', default=fields.Date.context_today)
    amount = fields.Float(string='Amount', digits=(16, 2), default=0.0)

    state = fields.Selection(
        selection=[('idle', 'Idle'),  ('busy', 'Busy'), ],
        string='Status', required=True, readonly=True, copy=False,
        default='idle')


class IotProjectHis(models.Model):
    _name = "iot.project.his"
    _description = 'project.his'
    _order = "id desc"

    project_id = fields.Many2one('iot.project', 'project_id')
    crop_type_id = fields.Many2one('iot.crop.type', 'crop_type_id')
    start_date = fields.Date('Start Date', default=fields.Date.context_today)
    end_date = fields.Date('End Date', default=fields.Date.context_today)
    amount = fields.Float(string='Amount', digits=(16, 2), default=0.0)
